<?php
namespace Index\Controller;
use Common\Common\Dateformat;
use Think\Controller;
class PeopleController extends Controller {
   public function indexAction(){
       $id = I('get.id');
       if(empty($id)){
            $this->error('非法操作',U('Index/Index/index'),1);
       }
       $uid = I('session.uid');
       $model = M();

       //用户信息
       $sql = "select views,id,face_url,username,introduce,province,city,job,views from user where id = '{$id}'";
       $userInfo = $model->query($sql);
       $userInfo =  $userInfo[0];
       $this->assign('userInfo',$userInfo);

       //个人网页访问量
       $userInfo['views'] = $userInfo['views'] + 1;
       $sql = "update user set views = '{$userInfo['views']}' where id = '{$id}'";
       $model->execute($sql);

       //关注问题的数量
       $sql = "select count(*) as Num from focus where uid = '{$id}'";
       $questionNum = $model->query($sql);
       $questionNum = $questionNum[0];
       $this->assign('questionNum',$questionNum);

       // 回复列表限制1条
       $sql = "select t1.id,t1.title,t2.content from question t1 inner join answer t2 on t1.id = t2.qid and t2.uid = '{$id}' order by t2.create_time desc limit 1";
       $answerData = $model->query($sql);
       $this->assign('answerData',$answerData);

       //回复列表全部展示
       $sql = "select t1.id,t1.title,t2.content,t2.create_time from question t1 inner join answer t2 on t1.id = t2.qid and t2.uid = '{$id}' order by t2.create_time desc";
       $moreAnswer = $model->query($sql);
       $answerCount= count($moreAnswer);
       $this->assign('moreAnswer',$moreAnswer);
       $this->assign('answerCount',$answerCount);

       //发问列表限制1条
       $sql = "select id,title,views,attention,create_time from question where uid='{$id}' order by create_time desc limit 1";
       $questionData = $model->query($sql);
       $this->assign('questionData',$questionData);

       //发问列表全部展示
       $sql = "select title,views,attention,create_time,id from question where uid='{$id}' order by create_time desc";
       $moreQuestion = $model->query($sql);
       $questionCount = count($moreQuestion);
       $this->assign('moreQuestion',$moreQuestion);
       $this->assign('questionCount',$questionCount);

       //最新动态列表限制1条
       $sql = "select t3.username,t3.id,t2.title,t1.create_time,t1.qid from answer t1 inner join question t2 on t1.qid = t2.id and t1.uid = '{$id}' inner join user t3 on t1.uid = t3.id order by t1.create_time desc limit 1";
       $dynamicData = $model->query($sql);
       $this->assign('dynamicData',$dynamicData);
       $dynamicCount = count($dynamicData);
       $this->assign('dynamicCount',$dynamicCount);


       //最新动态列表全部展示
       $sql = "select t3.username,t3.id,t2.title,t1.create_time,t1.qid from answer t1 inner join question t2 on t1.qid = t2.id and t1.uid = '{$id}' inner join user t3 on t1.uid = t3.id order by t1.create_time desc";
       $moreDynamic = $model->query($sql);
       $this->assign('moreDynamic',$moreDynamic);

       //判断是否关注人
       if(empty($uid)){
           $exist = 2;
       }else{
           if($uid == $id){
                 $exist = 3;
           }else{
                 $sql = "select * from attention where aid = '{$id}' and uid = '{$uid}' ";
                 $exist = $model -> query($sql);
                 $exist = empty($exist)? 0 : 1;
           }
           
       }
       $this->assign('exist',$exist);

       //某个人被多少人关注
       $sql = "select count(*) as count from attention where aid ='{$id}' ";
       $attend = $model->query($sql);
       $attend = $attend[0]['count'];
       $this->assign('attend',$attend);

       // 右边的关注的人数,限制8人
       $sql = "select t1.aid,t2.face_url from attention t1 inner join user t2 on t1.aid = t2.id and t1.uid= '{$id}' limit 8";
       $attendData = $model->query($sql);
       $this->assign('attendData',$attendData);
       $attendCount = count($attendData);
       $this->assign('attendCount',$attendCount);

       // 关注的人全部展示
       $sql = "select t1.aid,t2.username,t2.face_url,t2.introduce from attention t1 inner join user t2 on t1.aid = t2.id and t1.uid= '{$id}'";
       $attendTotal = $model->query($sql);
       $this->assign('attendTotal',$attendTotal);

       // 右边的被关注的人数,限制8人
       $sql = "select t1.uid,t2.face_url from attention t1 inner join user t2 on t1.uid = t2.id and t1.aid= '{$id}' limit 8";
       $beAttended = $model->query($sql);
       $this->assign('beAttended',$beAttended);
       $beAttendedCount = count($beAttended);
       $this->assign('beAttendedCount',$beAttendedCount);

       //被关注的人全部展示
       $sql = "select t1.uid,t2.username,t2.face_url,t2.job,t2.introduce from attention t1 inner join user t2 on t1.uid = t2.id and t1.aid= '{$id}'";
       $beAttendedTotal = $model->query($sql);
       $this->assign('beAttendedTotal',$beAttendedTotal);

       // 右边的被关注的问题,限制1条
       $sql = "select t2.title,t2.id from focus t1 inner join question t2 on t1.qid = t2.id and t1.uid ='{$id}' order by t2.create_time desc limit 1";
       $questionFocus = $model->query($sql);
       $this->assign('questionFocus',$questionFocus);
       $questionFocusnum = count($questionFocus);
       $this->assign('questionFocusnum',$questionFocusnum);

       // 被关注的问题全部展示
       $sql = "select t2.attention,t2.title,t2.id,t2.views,t2.create_time from focus t1 inner join question t2 on t1.qid = t2.id and t1.uid ='{$id}' order by t2.create_time desc";
       $questionTotal = $model->query($sql);
       $this->assign('questionTotal',$questionTotal);

       // 详细资料里的最后活跃
       $sql = "select logout_time,register_time,sex from user where id='{$id}'";
       $detail = $model->query($sql);
       $detail = $detail[0];
       $date = new Dateformat;
       $detail['logout_time'] = $date -> dateformat($detail['logout_time']);

       $this->assign('detail',$detail);

       $this->display('index');


    }



}